package org.lanqiao.webfm.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.lanqiao.webfm.model.RightsRole;
import org.lanqiao.webfm.model.RightsUser;
import org.lanqiao.webfm.service.RightsRoleService;
import org.lanqiao.webfm.service.RightsUserService;
import org.lanqiao.webfm.sys.SysConfig;
import org.lxh.smart.File;
import org.lxh.smart.Files;
import org.lxh.smart.SmartUpload;
import org.lxh.smart.SmartUploadException;

import javacommon.base.BaseServlet;
import javacommon.web.page.Page;
import javacommon.web.page.PageRequest;
import javacommon.web.util.ServletRequestUtils;

@WebServlet(urlPatterns = { "/RightsUser/*" })
public class RightsUserServlet extends BaseServlet {

	/**
	 * 展现用户信息
	 */
	@Override
	protected void doShowInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		String id = request.getParameter("id");
		RightsUser user = service.getById(id);
		request.setAttribute("model", user);
		request.getRequestDispatcher("/pages/RightsUser/show.jsp").forward(
				request, response);
		;
	}

	@Override
	protected void doSaveInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		try {
			RightsUser user = new RightsUser();
			SmartUpload smartUpload = new SmartUpload();
			String userIconPath = SysConfig.USER_ICON_PATH+UUID.randomUUID().toString()+"/";
			List<String> paths = upload(request, response, smartUpload,userIconPath);
			for (int i = 0; i < paths.size(); i++) {
				user.addIcon(paths.get(i));
			}
			// 快速地将request中的参数赋值给对象的方式,但是有一个要求,表单域的名字一定要和目标对象的属性相同
			//注意copy属性时,要从smart提供的request对象中拷贝,原始的request中无法取得表单数据(form-data)
			BeanUtils.copyProperties(
					user,
					ServletRequestUtils.getParametersStartingWith(
							smartUpload.getRequest(), ""));
			// 调用service
			service.save(user);
			// 跳转
			response.sendRedirect(request.getContextPath()
					+ "/RightsUser/list.do");
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(
					"beanUtils.copyProperties() error", e);
		} catch (InvocationTargetException e) {
			throw new IllegalArgumentException(
					"beanUtils.copyProperties() error", e.getTargetException());
		} catch (SmartUploadException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected void doCreateInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		// 查询出角色列表并共享给create.jsp
		List<RightsRole> roleList = roleService.findAll();
		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("/pages/RightsUser/create.jsp").forward(
				request, response);
	}

	@Override
	protected void doUpdateInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		try {
			RightsUser user = new RightsUser();
			int count = 0;
			SmartUpload smartUpload = new SmartUpload();
			String userIconPath = SysConfig.USER_ICON_PATH+UUID.randomUUID().toString()+"/";
			List<String> paths = upload(request, response, smartUpload,userIconPath);
			for (int i = 0; i < paths.size(); i++) {
				user.addIcon(paths.get(i));
			}
			// 快速地将request中的参数赋值给对象的方式,但是有一个要求,表单域的名字一定要和目标对象的属性相同
			beanUtils.copyProperties(user,
					ServletRequestUtils.getParametersStartingWith(smartUpload.getRequest(), ""));
			// 调用service
			service.update(user);
			// 跳转
			response.sendRedirect(request.getContextPath()
					+ "/RightsUser/show.do?id=" + user.getId());
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(
					"beanUtils.copyProperties() error", e);
		} catch (InvocationTargetException e) {
			throw new IllegalArgumentException(
					"beanUtils.copyProperties() error", e.getTargetException());
		} catch (SmartUploadException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 为修改数据做好准备
	 */
	@Override
	protected void doEditInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		String id = request.getParameter("id");
		RightsUser user = service.getById(id);
		// 准备model信息
		request.setAttribute("model", user);
		List<RightsRole> roleList = roleService.findAll();
		// 准备角色列表的信息
		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("/pages/RightsUser/edit.jsp").forward(
				request, response);
		;
	}

	@Override
	protected void doDeleteInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		// 1.获取表单数据,也就是用户选择的所有的id
		String[] ids = request.getParameterValues("items");
		service.removeByIds(ids);
		response.sendRedirect(request.getContextPath() + "/RightsUser/list.do");
	}

	// /RightsUser/list.do 准备展示系统用户列表
	@Override
	protected void doListInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		// 获取分页和搜索的参数
		// 获取数据
		// 共享数据
		// PageRequest pageRequest = new PageRequest();
		PageRequest<RightsUser> pageRequest = new RightsUser();
		populateQuery(pageRequest, "username desc", request);

		Page<RightsUser> page;
		page = service.findPage(pageRequest);
		savePage(page, pageRequest, request);
		List<RightsRole> roleList = roleService.findAll();
		request.setAttribute("roleList", roleList);
		// /webFramework/WebContent/pages/RightsUser/list.jsp
		request.getRequestDispatcher("/pages/RightsUser/list.jsp").forward(
				request, response);
		// response.sendRedirect("/webFramework/pages/RightsUser/list.jsp");
	}

	private RightsRoleService roleService = new RightsRoleService();
	private RightsUserService service = RightsUserService.getInstance();

}
